#include "friendlistmodel.h"
#include <QDir>

FriendListModel::FriendListModel(QObject *parent)
    :  QAbstractListModel(parent)
{
}

void FriendListModel::addBuddyItem(UserProfile* buddyItem)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_listBuddyItems.push_back(buddyItem);
    endInsertRows();
}

void FriendListModel::insertBuddyItem(UserProfile* buddyItem, const int pos)
{
    beginInsertRows(QModelIndex(), pos, pos);
    m_listBuddyItems.insert(pos,buddyItem);
    endInsertRows();
}


int FriendListModel::rowCount(const QModelIndex & parent) const {
    Q_UNUSED(parent);
    return m_listBuddyItems.count();
}

QVariant FriendListModel::data(const QModelIndex & index, int role) const {
    if (index.row() < 0 || index.row() >= m_listBuddyItems.count())
        return QVariant();

    const UserProfile* uProfile = m_listBuddyItems[index.row()];

    if (role == IdRole)
        return QVariant::fromValue<qulonglong>(uProfile->getUserId());
    else if (role == FullNameRole)
        return uProfile->getDisplayName();
    else if (role == DisplayNameRole)
        return uProfile->getDisplayName().trimmed();
    else if (role == FirstNameRole)
        return uProfile->getDisplayName();
    else if (role == LastNameRole)
        return uProfile->getDisplayName();
    else if (role == BirthdayRole)
        return "";
    //    return uProfile->getDisplayName();
    else if (role == MessageStatusRole)
        return uProfile->getMessageStatus();
    else if (role == EmailRole)
        return "";
   //     return buddyItem->email();
    else if (role == ProfileUrlRole)
        return uProfile->getAvatar();
    else if (role == AvatarUrlRole)
        return uProfile->getAvatar();
    else if (role == AvatarUrlLocalRole)
    {
        if(uProfile->getAvatarLocal() == ""){
            return "";
        }
        QString str = QDir::currentPath();
        str.append("/MeQml/Avatar/");
        return str.append(uProfile->getAvatarLocal());
    }
    else if (role == GenderRole) {
        return "Nam";
//        if (buddyItem->gender() == 0)
//            return QString("Nam");
//        else
            return QString("Nữ");
    }
    else if (role == PhoneRole) {
       // return buddyItem->profile().phone();
        return "";
    } else if (role == TimeStatusRole) {
        qint64 time = uProfile->getTime();

        QDateTime dateTime;
        dateTime.setMSecsSinceEpoch(time * 1000);
        int subTime = QDateTime::currentDateTime().date().day() - dateTime.date().day();

        QString timeString;
        if ( subTime  == 0) {
            timeString = dateTime.toString("hh:mm AP");
        } else {
            timeString = dateTime.toString("MMM d yy");
        }

        return timeString;

    } else if (role == TypeMsg) {
        return uProfile->getTypeMsg();
    } else if (role == FirstChar) {
        return QString(uProfile->getDisplayName()).left(1).toUpper();
    } else if (role == OnlineRole) {
        return uProfile->getOnline();
    } else if (role == TotalMember) {
        return uProfile->getTotalMember();
    }

    return QVariant();
}


QHash<int, QByteArray> FriendListModel::roleNames() const {
    QHash<int, QByteArray> roles;
    roles[IdRole] = "buddyId";
    roles[FullNameRole] = "fullName";
    roles[FirstNameRole] = "firstName";
    roles[LastNameRole] = "lastName";
    roles[DisplayNameRole] = "displayName";
    roles[BirthdayRole] = "birthday";
    roles[GenderRole] = "gender";
    roles[MessageStatusRole] = "messageStatus";
    roles[PhoneRole] = "phone";
    roles[EmailRole] = "email";
    roles[ProfileUrlRole] = "profileUrl";
    roles[AvatarUrlRole] = "avatarUrl";
    roles[AvatarUrlLocalRole] = "avatarUrlLocal";
    roles[TimeStatusRole] = "time";
    roles[TypeMsg] = "typeMsg";
    roles[FirstChar] = "firstChar";
    roles[OnlineRole] = "online";
    roles[TotalMember] = "totalMember";

    return roles;
}

void FriendListModel::insertUnitBuddy(const QList<UserProfile *> aUnitFriends)
{
    for (int i = 0; i < aUnitFriends.size(); i++ ){
        addBuddyItem(aUnitFriends.at(i));
    }


}

void FriendListModel::setListBuddy(const QList<UserProfile*> &buddyItems)
{
    beginResetModel();
    m_listBuddyItems = buddyItems;
    //qSort(m_listBuddyItems.begin(), m_listBuddyItems.end(), DisplayNameRole);
    endResetModel();
}

void FriendListModel::clear()
{
    beginResetModel();
    m_listBuddyItems.clear();
    endResetModel();
}


